package h0906;

/**
  * @description 合并两个有序链表
  * @author 不知名帅哥
  * @date 2024/9/6 21:36
  * @version 1.0
*/
public class MergeTwoSortedList {
    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public static void main(String[] args) {

    }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummy=new ListNode();
        ListNode preHead=dummy;

        while (list1!=null&&list2!=null){
            if (list1.val<=list2.val){
                preHead.next=list1;
                list1=list1.next;
            }else {
                preHead.next=list2;
                list2=list2.next;
            }
            preHead=preHead.next;
        }
        if (list1!=null){
            preHead.next=list1;
        }
        if (list2!=null){
            preHead.next=list2;
        }
        return dummy.next;
    }

}
